URL = "http://www.python.org/ftp/python/%(VERSION)s/Python-%(VERSION)s.tgz"
DEFAULT_VERSION = "2.7.6"
VERSIONS = ["2.6.5", "2.7.3", "2.7.6"]
REQUIRES = ["svn"]

#PYTHON_TARGET = "Python-2.6.5"
#PYTHON_LIB_VERSION = "2.6"
#PYTHON_LIB_VERSION_SHORT = "26"

#import build_generic
#from builders import *
#from machine import *
from source.builders import ConfigureMakeBuilder
import shutil, os

class PythonLinuxBuilder( ConfigureMakeBuilder ):

  def hasdebug( self ):
    return False

  def configureparameters( self ):
    # debug
    parameters = ["--with-pydebug", "--with-suffix=_d"] if self.debug else []
    # shared
    parameters += ["--" + ("enable" if self.runtime.build_linktype == "shared" else "disable") + "-shared"]
    # unicode
    parameters += ["--enable-unicode="+("ucs2","ucs4")[self.machine.bits == 64]]
    return parameters

  def libversion(self):
    return self.version[:3]

  def libversionshort(self):
    return self.version[0] + self.version[2]

  def getincludedir(self):
    return ConfigureMakeBuilder.getincludedir(self) + os.sep + "python" + self.libversion() 



class PythonWindowsBuilder( ConfigureMakeBuilder ):

  def hasdebug( self ):
    return True

  def configure( self ):
    os.chdir( self.extractedpath )

  def make(self):
    os.chdir( self.extractedpath )
    externalbat = {32 : "external.bat", 64 : "external-amd64.bat" }
    buildtype = {32 : '"Release|win32"', 64 : '"Release|x64"' }    
    buildtype_debug = {32 : '"Debug|win32"', 64 : '"Debug|x64"' }
    self.execute( "Tools\\buildbot\\" + externalbat[self.machine.bits] )

    if not self.machine.isVS2008():
      #to build with the platform sdk installed with vs2005
      add_windows = [("#define Py_CONFIG_H", "#define Py_CONFIG_H\n#include <windows.h>\n" )]
      self.replaceLinesInFile( "PC\\pyconfig.h", add_windows )


    basefolder = "PC\\VS8.0\\"
    if self.machine.isVS2008():
      basefolder = "PCbuild\\"

    buildtype_string = ( buildtype[self.machine.bits], buildtype_debug[self.machine.bits] )[self.debug]
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"pythoncore.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"python.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"_sqlite3.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"_socket.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"_ctypes.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"unicodedata.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"pyexpat.vcproj\" " )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"select.vcproj\" " )

    myenv = os.environ.copy()
    myenv["PATH"] += os.pathsep + os.pathsep.join(["C:\\Python27\\", "C:\\Python26\\"])
    myenv["HOST_PYTHON"] = "python.exe"
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"_ssl.vcproj\" ", myenv )
    self.execute( "devenv "+basefolder+"pcbuild.sln /Build "+ buildtype_string+" /project \"_multiprocessing.vcproj\" ", myenv )

  def libversion(self):
    return self.version[:3]

  def libversionshort(self):
    return self.version[0] + self.version[2]
 
  def install(self):

    if self.debug:
      if os.path.exists(self.targetpath()):
        #remove the target path
        shutil.rmtree( self.targetpath() )
        #create a new one
        os.makedirs( self.targetpath() )
      
      print("only install after release is built..")
      return
    
    basefolder = "PC\\VS8.0\\"
    if self.machine.isVS2008():
      basefolder = "PCbuild\\"
    
    os.chdir( self.extractedpath )
    libdir = {32 : "", 64 : "amd64\\" }    

    self.copyfile( "LICENSE", self.targetpath() )
    #self.copydir( "Include", self.targetpath + "\\include\\python" + self.libversion() )
    #self.copyfile( "PC\\pyconfig.h", self.targetpath + "\\include\\python" + self.libversion() )
    #self.copydir( "Lib", self.targetpath + "\\lib\\python"+ self.libversion() ) #todo on windows install to \Lib
    self.copydir( "Include", self.targetpath() + "\\include" )
    self.copyfile( "PC\\pyconfig.h", self.targetpath() + "\\include" )
    self.copydir( "Lib", self.targetpath() + "\\lib" ) 
    for dir in ["i18n", "pynche", "Scripts", "versioncheck", "webchecker"]:
      self.copydir( "Tools\\" + dir, self.targetpath() + "\\Tools\\" + dir)

    #postfix = "_d" if self.debug else ""
    for postfix in ["", "_d"]:
      self.copyfile( basefolder+libdir[self.machine.bits]+"python"+self.libversionshort()+postfix+".lib", self.targetpath() + "\\libs" ) 
      self.copyfile( basefolder+libdir[self.machine.bits]+"python"+self.libversionshort()+postfix+".dll", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"_sqlite3"+postfix+".pyd", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"_socket"+postfix+".pyd", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"_ctypes"+postfix+".pyd", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"unicodedata"+postfix+".pyd", self.targetpath() + "\\bin" )      
      self.copyfile( basefolder+libdir[self.machine.bits]+"pyexpat"+postfix+".pyd", self.targetpath() + "\\bin" )      
      self.copyfile( basefolder+libdir[self.machine.bits]+"select"+postfix+".pyd", self.targetpath() + "\\bin" )      
      self.copyfile( basefolder+libdir[self.machine.bits]+"sqlite3"+postfix+".dll", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"python"+postfix+".exe", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"_ssl"+postfix+".pyd", self.targetpath() + "\\bin" )
      self.copyfile( basefolder+libdir[self.machine.bits]+"_multiprocessing"+postfix+".pyd", self.targetpath() + "\\bin" )

    remove_debug_lib = [
        ('#\t\t\t\tpragma comment(lib,"python27_d.lib")', '#\t\t\t\tpragma comment(lib,"python27.lib")\n'),
        ('#\tdefine Py_DEBUG', '//#\tdefine Py_DEBUG' )
        ] 

    self.replaceInFile( self.targetpath() + "\\include\\pyconfig.h", remove_debug_lib )



def decider(machine, directories ):
  if machine.isWindows():
    return PythonWindowsBuilder
  else:
    return PythonLinuxBuilder

BUILDER = decider


def getpythondir( machine, directories):
  if machine.isWindows():
    return directories.enviro + "\\" + PYTHON_TARGET
  else:
    return directories.enviro + "/" + PYTHON_TARGET
